// components/teamlist/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    showApply:{
      type:Boolean,
      value:false
    },
    teamlist:{
      type: Array,
      value:[]
    },
    delable:{
      type:Boolean,
      value:false
    },
    delapp:{
      type:Boolean,
      value:false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    showMember:false,
    showModal:false,
    memberlist:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    goteamDetail:function (e) {
      let id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: `/pages/teamDetail/index?id=${id}`,
      })
    },
    preimg:function(event){
      let index = event.currentTarget.dataset.index;
      let src=this.data.teamlist[index].teamimg;
  
      wx.previewImage({
        current:src,
        urls: [src],
      })
    },
    linkteam:function (e) {
      let index = e.currentTarget.dataset.index;
      let tele=this.data.teamlist[index].phone;
      wx.showModal({
        title: '联系电话为：',
        content: tele,
        showCancel: true,
        cancelText: '取消',
        cancelColor: '#000000',
        confirmText: '复制',
        confirmColor: '#3CC51F',
        success: (result) => {
          if (result.confirm) {
            wx.setClipboardData({
              data:tele,
            })
  
          }
        },
        fail: () => {
          return false
        },
        complete: () => {},
      })
   
    },
    addteam: function (e) {
      let teamid = e.currentTarget.dataset.teamid, teamtype = e.currentTarget.dataset.type
      let openid = e.currentTarget.dataset.openid
      let member = e.currentTarget.dataset.member
      let meopenid = wx.getStorageSync('OPENID')
      if (meopenid === openid) {
        wx.showToast({
          title: '不能加入自己发布的组队！',
          icon: 'none',
          duration: 2000,
        })
        return
      }
      let ismember=false 
      member.forEach(item=>{
        if(item.openid==meopenid){
          ismember = true
        }
      })
      if (ismember) {
        wx.showToast({
          title: '不能重复加入一个队伍！',
          icon: 'none',
          duration: 2000,
        })
        return
      }

      wx.showModal({
        title: '提示',
        content: '确定申请吗？',
        showCancel: true,
        cancelText: '取消',
        cancelColor: '#000000',
        confirmText: '确定',
        confirmColor: '#3CC51F',
        success: (result) => {
          if (result.confirm) {
            wx.navigateTo({
              url: `/pages/application/application?teamid=${teamid}&teamtype=${teamtype}`,
            })
  
          }
        },
        fail: () => {
          return false
        },
        complete: () => {},
      })
    },
    reject: function (e) {
      let applyid = e.currentTarget.dataset.applyid
      console.log(applyid)
      this.setData({
        showModal: false,
      })
      db.collection('applications').where({_id: applyid}).remove()
    },
    agree: function(e){
      let that = this
      let teamid = e.currentTarget.dataset.teamid
      let memberid = e.currentTarget.dataset.memberid, memberwxid=e.currentTarget.dataset.memberwxid, membernickname=e.currentTarget.dataset.membernickname
      let member= e.currentTarget.dataset.member
      let vacancy = e.currentTarget.dataset.vacancy
      let applyid = e.currentTarget.dataset.applyid
      const db = wx.cloud.database()

      let ismember=false 
      member.forEach(item=>{
        if(item.openid==memberid){
          item.wxid = memberwxid
          item.nickname = membernickname
          ismember = true
        }
      })
      if (ismember) {
        wx.showToast({
          title: '该用户已加入',
          icon: 'none',
          duration: 2000,
          success:(res)=>{
            that.setData({
              showModal: false
            })
          }
        })
        db.collection('applications').where({_id: applyid}).remove()
        return
      }
        member.push({"openid":memberid,"nickname":membernickname,"wxid":memberwxid})
                db.collection('teamlist').doc(teamid).update({
              data: {
                vacancy: vacancy - 1,
                member: member,
              },
              success: function (res) {
                try {
                  db.collection('applications').where({_id: applyid}).remove()
                  wx.showToast({
                    title: '审批成功',
                    icon: 'none',
                    image: '',
                    duration: 1500,
                    mask: false,
                    success: (result) => {},
                    fail: () => {},
                    complete: () => {
                      that.setData({
                        showModal: false
                      })
                      this.triggerEvent('refreshTeam')
                    },
                  })
                } catch (error) {
                  console.error(error)
                }
                
                
  
              },
            })
    },
    showapplication: function() {
      this.setData({
        showModal:true
      })
    },
    showMembers:function(e){
      let members = e.currentTarget.dataset.member
      this.setData({
        memberlist:members,
        showMember:true
      })
      console.log(this.data.memberlist)

    },
    modal_click_Hidden:function(){
      this.setData({
        showMember:false,
        showModal:false
      })
    },
    delteam:function(e){
      let teamid = e.currentTarget.dataset.teamid
      wx.showModal({
        title: '提示',
        content: '确定删除该队伍吗？',
        showCancel: true,
        cancelText: '取消',
        confirmText: '确定',
        success: (result) => {
          if (result.confirm) {
            wx.cloud.database().collection('teamlist').where({_id:teamid}).remove({
              success:res=>{
                wx.showToast({
                  title: '删除成功',
                })
              }
            })
          }
        },
        fail: () => {
          return false
        },
        complete: () => {},
      })

    },
    delapp:function(e){
      let appid = e.currentTarget.dataset.appid
      wx.showModal({
        title: '提示',
        content: '确定删除该申请吗？',
        showCancel: true,
        cancelText: '取消',
        confirmText: '确定',
        success: (result) => {
          if (result.confirm) {
            wx.cloud.database().collection('applications').where({_id:appid}).remove({
              success:res=>{
                wx.showToast({
                  title: '删除成功',
                })
              }
            })
          }
        },
        fail: () => {
          return false
        },
        complete: () => {},
      })
    }
  }
})
